package com.Nahida.NaHome.activity.base;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.Nahida.NaHome.Application;
import com.Nahida.NaHome.R;
import com.Nahida.NaHome.util.MsgUtil;
import com.Nahida.NaHome.util.SharedPreferencesUtil;


public class BaseActivity extends AppCompatActivity {
    public Context old_context;

    //调整应用内dpi的代码，其他Activity要继承于BaseActivity才能调大小
    @Override
    protected void attachBaseContext(Context newBase) {
        old_context = newBase;
        super.attachBaseContext(Application.getFitDisplayContext(newBase));
    }

    //调整页面边距，参考了hankmi的方式
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
        int density;
        if ((density = SharedPreferencesUtil.getInt("density", -1)) >= 72) {
            setDensity(density);
        }
         */
    }

    @Override
    protected void onStart() {
        super.onStart();
        setTopExit();
    }

    public void setDensity(int targetDensityDpi) {
        Resources resources = getResources();

        if (resources.getConfiguration().densityDpi == targetDensityDpi) return;

        Configuration configuration = resources.getConfiguration();
        configuration.densityDpi = targetDensityDpi;
        configuration.fontScale = 1f;
        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
    }

    public void setTopExit(){
        View view = findViewById(R.id.exit);
        if(view != null) view.setOnClickListener(view1 -> finish());
    }

    public void setPageName(String name) {
        TextView textView = findViewById(R.id.pageName);
        if (textView != null) textView.setText(name);
    }

    public void report(Exception e) {
        runOnUiThread(() -> MsgUtil.err(e, this));
    }
}
